import re
from common.readData import ReadData
from common.log import logger
from common.configHttp import configHttp
import jsonpath

#定义一个类
class Presovlve:
    #定义初始化方法
    def __init__(self,testdata):
        self.testdata = testdata

    def presolve(self,id,rely,caseid,header,value):
        logger.debug(f"id:{id},testdata:{self.testdata}")
        print(rely,caseid,header,value)
        if rely.lower() == "y" and id != "1" and caseid !="":
            goal_h= self.get_predata(header)
            goal_b= self.get_predata(value)

            #print("调用自定义方法找到依赖字段")
            h,b = self.run_pre(caseid,goal_h, goal_b)
            #print("调用执行前置用例方法,获得依赖字段")
            if h != None:
                header = header.replace("${"+goal_h+"}",h)
            if b !=None:
                value = value.replace("${"+goal_b+"}",b)
            # print("替换数据获得具体依赖的头/体")
            # print("返回替换后的头/体")
            return header,value
        else:
            print("返回原始头/体")
            return header,value

    def get_predata(self,data):
        res = re.findall(r"\$\{(.*?)\}",data)
        if len(res) != 0:
            res_data  = res[0]

        else:
            res_data = None

        return res_data

    def run_pre(self,caseid,goal_h=None,goal_b=None):
        preUrl = self.testdata[int(caseid)-1]["interfaceUrl"]
        premethod = self.testdata[int(caseid)-1]["method"]
        prevalue = self.testdata[int(caseid)-1]["value"]
        preheader = self.testdata[int(caseid)-1]["header"]

        ch = configHttp(premethod,preUrl,prevalue,preheader)
        res = ch.run()

        if goal_h != None:
            goal_h = res.headers[goal_h]

    #jsonpath.jsonpath(res.json())具体看字母导包,如果导包是from jsonpath import jsonpath  那么此处就是jsonpath(res.json())
    #如果导包是import jsonpath  那么此处就是jsonpath.jsonpath(res.json())---包名.方法名()
        if goal_b != None:
            goal_b = jsonpath.jsonpath(res.json(),"$.."+goal_b)[0]  #此处jsonpath 返回的是一个列表[0]列表中的第一个数放外面

        return goal_h,goal_b



if __name__ == '__main__':
    """
    {'id': '1', 'interfaceUrl': 'https://www.wanandroid.com/user/login', 'name': 'login', 'method': 'post', 'value': "{'username':'liangchao','password':'123456'}", 'header': '{}', 'expect': 0.0}, 
    {'id': '2', 'interfaceUrl': 'https://www.wanandroid.com/user/register', 'name': 'register', 'method': 'post', 'value': "{'username':'liangchao03','password':'123456','repassword':'123456'}", 'header': '{}', 'expect': 0.0}, 
    {'id': '3', 'interfaceUrl': 'https://www.wanandroid.com/user/logout/json', 'name': 'logout', 'method': 'get', 'value': "{'username':'liangchao'}", 'header': '{}', 'expect': '0'}
    """
    data = ReadData()
    data = data.read_excel()

    ps = Presovlve(data)
    id = "5"
    caseid = "1"
    rely = "y"
    header = '{"cookie":"${Set-Cookie}"}'
    value = "{'name':'${username}','link':'www.baidu.com'}"
    print(ps.presolve(id, rely, caseid, header, value))

    #调试
    # data1 = "{'username':'liangchao','password':'123456'}"
    # data2 = '{"cookie":"${Set-Cookie}"}'
    # data3 = "{'name':'${username}','link':'www.baidu.com'}"
    # print(ps.get_predata(data1))
    # print(ps.get_predata(data2))
    # print(ps.get_predata(data3))

    #print(ps.run_pre(1, "Set-Cookie", "username"))















